﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Globalization;
using System.Linq;
using EstateNuke.Data.Properties;

namespace EstateNuke.Data
{
    public partial class LargeOrder
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        public LargeOrderEngagedContractAlert LatestUnProcessedLargeOrderEngagedContractAlert
        {
            get
            {
                return InternalLargeOrderEngagedContractAlerts.FirstOrDefault(largeOrderEngagedContractAlert => largeOrderEngagedContractAlert.IsDeleted == false && largeOrderEngagedContractAlert.Processed == false);
            }
        }

        /// <summary>
        /// </summary>
        public IList<LargeOrderEngagedContractAlert> LargeOrderEngagedContractAlerts
        {
            get
            {
                return InternalLargeOrderEngagedContractAlerts.Where(largeOrderEngagedContractAlert => largeOrderEngagedContractAlert.IsDeleted == false).ToList();
            }
        }

        #endregion

        #region Create & Remove LargeOrderEngagedContractAlert

        /// <summary>
        /// </summary>
        /// <param name = "alertType"></param>
        /// <param name = "modifiedUser"></param>
        public LargeOrderEngagedContractAlert CreateLargeOrderEngagedContractAlert(AlertType alertType, User modifiedUser)
        {
            if (alertType == null)
            {
                throw new ArgumentNullException("alertType");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (alertType.Id != 30)
            {
                throw new ArgumentException("alertType");
            }
            if (!AgreementReference.IsLoaded)
            {
                AgreementReference.Load(MergeOption.PreserveChanges);
            }
            var reminderDays = alertType.DefaultReminderDays.HasValue ? alertType.DefaultReminderDays : 0;
            var alertTypeReminderDays = Agreement.Project.AlertTypeReminderDays;
            if (alertTypeReminderDays != null && alertTypeReminderDays.ContainsKey(alertType) && alertTypeReminderDays[alertType] != null && alertTypeReminderDays[alertType].Item1.HasValue)
            {
                reminderDays = alertTypeReminderDays[alertType].Item1;
            }
            if (reminderDays != 0)
            {
                var largeOrderEngagedContractAlert = InternalLargeOrderEngagedContractAlerts.FirstOrDefault(item => item.IsDeleted == false && item.Processed == false);
                if (largeOrderEngagedContractAlert == null)
                {
                    largeOrderEngagedContractAlert = new LargeOrderEngagedContractAlert
                                                         {
                                                             RowId = Guid.NewGuid(),
                                                             CreatedDate = DateTime.Now,
                                                         };
                    InternalLargeOrderEngagedContractAlerts.Add(largeOrderEngagedContractAlert);
                }
                largeOrderEngagedContractAlert.IsDeleted = false;
                largeOrderEngagedContractAlert.ModifiedDate = DateTime.Now;
                largeOrderEngagedContractAlert.ModifiedByUser = modifiedUser;
                largeOrderEngagedContractAlert.AlertType = alertType;
                largeOrderEngagedContractAlert.RemindDate = EngagedContractDate.AddDays((double) reminderDays);
                return largeOrderEngagedContractAlert;
            }
            return null;
        }

        /// <summary>
        /// </summary>
        /// <param name = "largeOrderEngagedContractAlert"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveLargeOrderEngagedContractAlert(LargeOrderEngagedContractAlert largeOrderEngagedContractAlert, User modifiedUser)
        {
            if (largeOrderEngagedContractAlert == null)
            {
                throw new ArgumentNullException("largeOrderEngagedContractAlert");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalLargeOrderEngagedContractAlerts.Contains(largeOrderEngagedContractAlert))
            {
                largeOrderEngagedContractAlert.Delete(modifiedUser);
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            if (string.IsNullOrEmpty(OrderNumber))
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NotNullAllowed, RowId, "LargeOrder", "OrderNumber"));
            }
            if (ReducedUnitPrice < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LargeOrder", "ReducedUnitPrice"));
            }
            if (ReducedTotalPrice < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LargeOrder", "ReducedTotalPrice"));
            }
            if (DiscountRate < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LargeOrder", "DiscountRate"));
            }
            if (TotalAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LargeOrder", "TotalAmount"));
            }
            if (DepositAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LargeOrder", "DepositAmount"));
            }
            if (FirstInstalmentAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LargeOrder", "FirstInstalmentAmount"));
            }
            if (MortgageAmount < 0)
            {
                throw new EntityException(string.Format(CultureInfo.CurrentCulture, Resources.NonNegativeNumberAllowed, RowId, "LargeOrder", "MortgageAmount"));
            }
            OrderNumber = OrderNumber.Length > 32 ? OrderNumber.Substring(0, 32) : OrderNumber;
            EngagedContractDate = EngagedContractDate.Date;
            Comment = string.IsNullOrEmpty(Comment) ? null : Comment.Length > 512 ? Comment.Substring(0, 512) : Comment;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<LargeOrderEngagedContractAlert>) LargeOrderEngagedContractAlerts).ForEach(largeOrderEngagedContractAlert => largeOrderEngagedContractAlert.Delete(modifiedUser));
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}